//
//  JUImageGalleryItem.m
//  JUImageGalleryDemo
//
//  Created by Ju on 15/8/19.
//  Copyright (c) 2015年 juwenz. All rights reserved.
//

#import "JUImageGalleryStepViewController.h"
#import "JUImageScollView.h"

@interface JUImageGalleryStepViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) JUImageScollView *scrollView;

@end

@implementation JUImageGalleryStepViewController

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.scrollView];
    [self loadImage];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.scrollView.frame = self.view.bounds;
}

- (void)dealloc {
    if ([self.loaderDelegate respondsToSelector:@selector(cancelRequestWithImageURI:)]) {
        [self.loaderDelegate cancelRequestWithImageURI:self.imageURI];
    }
}

#pragma mark - IBActions


#pragma mark - 公开方法


#pragma mark - 协议


#pragma mark - 私有方法
- (void)loadImage {
    switch (self.type) {
        case JUImageGalleryImageFromBundle: {
            if (self.imageURI
                && ![self.imageURI isEqualToString:@""]) {
                UIImage *image = [UIImage imageNamed:self.imageURI];
                [self displayImage:image];
            }
            break;
        }
        case JUImageGalleryImageFromLocalPath: {
            break;
        }
        case JUImageGalleryImageFromServer: {
            __weak JUImageGalleryStepViewController *weakSelf = self;
            if ([self.loaderDelegate respondsToSelector:@selector(requsetImageWithImageURI:successCallback:failureCallback:)]) {
                [self.loaderDelegate requsetImageWithImageURI:self.imageURI successCallback:^(id request, UIImage *image) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [weakSelf displayImage:image];
                    });
                } failureCallback:^(id request, NSString *error) {
                    //default or show error
                    
                }];
            }
            break;
        }
    }
}

- (void)displayImage:(UIImage *)image {
    [self.scrollView setImage:image];
}

#pragma mark - 属性
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[JUImageScollView alloc] init];
        _scrollView.directionalLockEnabled = YES;
        //一定要设置delegate
    }
    return _scrollView;
}

- (void)setImageURI:(NSString *)imageURI {
    _imageURI = imageURI;
//    [self loadImage];
}


@end
